package com.demo.fieldbind;

import cn.lingyangwl.framework.data.mate.fieldbind.inter.IDataBind;
import cn.lingyangwl.framework.data.mate.fieldbind.model.DictType;
import com.alibaba.fastjson2.JSON;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author shenguangyang
 */
@Component
public class DataDictBind implements IDataBind<Map<String, Map<String, String>>> {
    public static final Map<String, String> EMPTY_MAP = new HashMap<>();

    /**
     * 通过对象中所有字典type获取每个type对应的code数据集
     * @param dictTypes 字典类型集合, 所有对象同一个字典类型会被转成一个 {@link DictType}对象的,
     *                  同时内部也对注解所在的字段值进行了去重
     * @return types对应的类型集合, 一般情况下会返回一个map集合, key = type, value = valueOfType(或者集合)
     */
    @Override
    public Map<String, Map<String, String>> getCodesOfTypes(List<DictType> dictTypes) {
        Set<String> types = dictTypes.stream().map(DictType::getType).collect(Collectors.toSet());
        System.err.println(Thread.currentThread().getName() + " -- getValuesOfTypes: " + JSON.toJSONString(dictTypes));
        // 从数据库或缓存中获取, 一次性全部取出或者根据code进行精确查找
        Map<String, String> codeMap = new HashMap<String, String>() {{
            put("1", "状态1");
            put("2", "状态2");
            put("3", "状态3");
            put("4", "状态4");
            put("5", "状态5");
            put("6", "状态6");
            put("7", "状态7");
            put("8", "状态8");
        }};
        Map<String, Map<String, String>> respMap = new HashMap<>();
        for (String type : types) {
            respMap.put(type, codeMap);
        }
        return respMap;
    }

    @Override
    public String getCodeValue(Map<String, Map<String, String>> dictMap, String type, String code) {
        return dictMap.getOrDefault(type, new HashMap<>()).get(code);
    }
}
